//package com.tezign.intelligence.mini.dam.aifamilyconsultant.util;
//
//import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChunk;
//import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
//import okhttp3.MediaType;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.RequestBody;
//import okhttp3.Response;
//import okhttp3.sse.EventSource;
//import okhttp3.sse.EventSourceListener;
//import okhttp3.sse.EventSources;
//import com.google.gson.Gson;
//import com.google.gson.JsonObject;
//import com.google.gson.JsonArray;
//import io.reactivex.Flowable;
//import io.reactivex.rxjava3.core.FlowableEmitter;
//import io.reactivex.rxjava3.core.BackpressureStrategy;
//import org.jetbrains.annotations.NotNull;
//
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//public class DeepSeekClient {
//    private static final String API_BASE_URL = "https://api.deepseek.com/v1";
//    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
//
//    private final OkHttpClient client;
//    private final Gson gson;
//    private final String apiKey;
//
//    public DeepSeekClient(String apiKey) {
//        this.apiKey = apiKey;
//        this.gson = new Gson();
//        this.client = new OkHttpClient.Builder()
//                .connectTimeout(30, TimeUnit.SECONDS)
//                .readTimeout(60, TimeUnit.SECONDS)
//                .writeTimeout(60, TimeUnit.SECONDS)
//                .build();
//    }
//
//    public Flowable<ChatCompletionChunk> streamChatCompletion(List<ChatMessage> messages) {
//        return Flowable.create(emitter -> {
//            EventSource eventSource = null;
//
//            try {
//                JsonObject requestBody = new JsonObject();
//                requestBody.addProperty("model", "deepseek-chat");
//                requestBody.addProperty("stream", true);
//
//                JsonArray messagesArray = new JsonArray();
//                for (ChatMessage message : messages) {
//                    JsonObject msgObj = new JsonObject();
//                    msgObj.addProperty("role", message.getRole().name());
//                    msgObj.addProperty("content", (String) message.getContent());
//                    messagesArray.add(msgObj);
//                }
//                requestBody.add("messages", messagesArray);
//
//                Request request = new Request.Builder()
//                        .url(API_BASE_URL + "/chat/completions")
//                        .header("Authorization", "Bearer " + apiKey)
//                        .header("Accept", "text/event-stream")
//                        .post(RequestBody.create(requestBody.toString(), JSON))
//                        .build();
//
//                EventSourceListener listener = new EventSourceListener() {
//                    @Override
//                    public void onEvent(@NotNull EventSource eventSource, String id, String type, @NotNull String data) {
//                        if ("[DONE]".equals(data)) {
//                            emitter.onComplete();
//                            return;
//                        }
//
//                        try {
//                            ChatCompletionChunk chunk = gson.fromJson(data, ChatCompletionChunk.class);
//                            emitter.onNext(chunk);
//                        } catch (Exception e) {
//                            emitter.onError(e);
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(@NotNull EventSource eventSource, Throwable t, Response response) {
//                        emitter.onError(t != null ? t : new RuntimeException("SSE failure"));
//                    }
//                };
//
//                eventSource = EventSources.createFactory(client).newEventSource(request, listener);
//
//                EventSource finalEventSource = eventSource;
//                emitter.setCancellable(() -> {
//                    if (finalEventSource != null) {
//                        finalEventSource.cancel();
//                    }
//                });
//            } catch (Exception e) {
//                emitter.onError(e);
//                if (eventSource != null) {
//                    eventSource.cancel();
//                }
//            }
//        }, BackpressureStrategy.BUFFER);
//    }
//
//    public void close() {
//        client.dispatcher().executorService().shutdown();
//        client.connectionPool().evictAll();
//    }
//}